package com.navinfo.platform.dataanalysis.dto;

import com.navinfo.location.mileage.bean.Point;
import com.navinfo.platform.common.dto.DataInfo;
import com.navinfo.platform.common.utils.CommonUtil;
import com.navinfo.stream.qingqi.protocol.java.LCStatusType;
import lombok.Data;
import org.apache.spark.sql.Row;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

@Data
public class LocationDataPoint implements DataInfo, Serializable {
    /**
     * GPS里程
     */
    private double gpsMileage;
    /**
     * acc
     */
    private int acc = 0;

    /**
     * 补传标识
     */
    private boolean isPatch;
    /**
     * 转速
     */
    private int rotation = 0;
    /**
     * GPS时间
     */
    private long gpsDate;
    /**
     * 标准速度
     */
    private int speed;
    /**
     * 通信号
     */
    private long tid;
    /**
     * 仪表里程
     */
    private double meterMileage;
    /**
     * 积分里程
     */
    private double diffMileage;
    /**
     * ecu里程
     */
    private double ecuMileage;
    /**
     * 积分油耗
     */
    private double diffFuelCons;
    /**
     * 燃油总消耗
     */
    private double totalFuelCons;
    /**
     * PTO
     */
    private long engineTorMode;
    /**
     * 油门开度
     */
    private int accPedalPos;
    /**
     * 海拔
     */
    private double height;
    private int lat;
    private int lng;

    /**
     * 扭矩
     */
    private int actualEnginePercentTor;
    /**
     * 冷却水温
     */
    private int coolingWaterTem;

    /**
     * 进气温度
     */
    private int intakeAirTem;
    /**
     * 进气压力
     */
    private int dpfPressure;
    /**
     * 离合开关
     */
    private int clutchSwitch;

    private int brake;

    public static LocationDataPoint parseObj(Row row) {
        if (row == null) {
            return null;
        }
        double gpsMileage;
        //当前转速
        int rotation = 0;
        //仪表里程
        double mileageDD = 0d;
        //can里程(ecu)
        double ecuMileage = 0d;
        // 积分里程
        double differentialMileage = 0d;
        //燃油总消耗
        double totalFuelConsumption = 0d;
        //积分油耗
        double integralFuelConsumption = 0d;
        long engineTorMode = 0;
        int accPedalPos = 0;
        int coolingWaterTem = 0;
        int intakeAirTem = 0;
        int dpfPressure = 0;
        int clutchSwitch = 0;
        //实际发动机扭矩百分比
        int actualEnginePercentTor = 0;
        //GPS里程
        BigDecimal bigDecimal = new BigDecimal(Double.parseDouble(row.getAs("mileage"))).divide(
                new BigDecimal("1000"), 0, BigDecimal.ROUND_HALF_UP);
        gpsMileage = bigDecimal.doubleValue();
        int acc = (int) CommonUtil.getToBinary(Long.parseLong(row.getAs("status")), 0);
        Long tid = Long.parseLong(row.getAs("tid"));
        boolean patch = Boolean.parseBoolean(row.getAs("ispatch"));
        long gpsDate = Long.parseLong(row.getAs("gpsdate"));
        double height = Double.parseDouble(row.getAs("height"));
        int lat = Integer.parseInt(row.getAs("originallat"));
        int lng = Integer.parseInt(row.getAs("originallng"));
        List<VehicleStatusAdditionDto> statusAdditionDtos = VehicleStatusAdditionDto.parseToList(
                row.getAs("statusaddition"));

        //取标准速度
        int speed = (int) Long.parseLong(row.getAs("electricvehicle")) / 100;
        if (speed == 0) {
            //标准速度为0取gps速度
            speed = Integer.parseInt(row.getAs("speed"));
        }
        for (VehicleStatusAdditionDto dto : statusAdditionDtos) {
            if (Integer.parseInt(dto.getTypes()) == LCStatusType.StatusType.rotation_VALUE) {
                //转速
                rotation = new BigDecimal(dto.getStatusValue()).
                        divide(new BigDecimal("100"), 0, BigDecimal.ROUND_HALF_UP).intValue();
            } else if (Integer.parseInt(dto.getTypes()) == LCStatusType.StatusType.mileage_VALUE) {
                //can里程(ecu)
                ecuMileage = (double) dto.getStatusValue() / 100;
            } else if (Integer.parseInt(dto.getTypes()) == LCStatusType.StatusType.mileageDD_VALUE) {
                //仪表里程
                mileageDD = (double) dto.getStatusValue() / 100;
            } else if (Integer.parseInt(
                    dto.getTypes()) == LCStatusType.StatusType.differentialMileage_VALUE) {
                // 积分里程
                differentialMileage = (double) dto.getStatusValue() / 100;
            } else if (Integer.parseInt(
                    dto.getTypes()) == LCStatusType.StatusType.totalFuelConsumption_VALUE) {
                //燃油总消耗
                totalFuelConsumption = (double) dto.getStatusValue() / 100;
            } else if (Integer.parseInt(
                    dto.getTypes()) == LCStatusType.StatusType.integralFuelConsumption_VALUE) {
                //积分油耗
                integralFuelConsumption = (double) dto.getStatusValue() / 100;
            } else if (Integer.parseInt(dto.getTypes()) == LCStatusType.StatusType.engineTorMode_VALUE) {
                engineTorMode = dto.getStatusValue() / 100;
            } else if (Integer.parseInt(dto.getTypes()) == LCStatusType.StatusType.accPedalPos_VALUE) {
                accPedalPos = (int) dto.getStatusValue() / 100;
            } else if (Integer.parseInt(dto.getTypes()) == LCStatusType.StatusType.actualEnginePercentTor_VALUE) {
                actualEnginePercentTor = (int) dto.getStatusValue() / 100;
            } else if (Integer.parseInt(dto.getTypes()) == LCStatusType.StatusType.coolingWaterTem_VALUE) {
                coolingWaterTem = (int) dto.getStatusValue() / 100;
            } else if (Integer.parseInt(dto.getTypes()) == LCStatusType.StatusType.intakeAirTem_VALUE) {
                intakeAirTem = (int) dto.getStatusValue() / 100;
            } else if (Integer.parseInt(dto.getTypes()) == LCStatusType.StatusType.dpfPressure_VALUE) {
                dpfPressure = (int) dto.getStatusValue() / 100;
            } else if (Integer.parseInt(dto.getTypes()) == LCStatusType.StatusType.clutchSwitch_VALUE) {
                clutchSwitch = (int) dto.getStatusValue() / 100;
            }
        }

        LocationDataPoint locationDataPoint = new LocationDataPoint();
        locationDataPoint.setTid(tid);
        locationDataPoint.setAcc(acc);
        locationDataPoint.setPatch(patch);
        locationDataPoint.setSpeed(speed);
        locationDataPoint.setGpsDate(gpsDate);
        locationDataPoint.setGpsMileage(gpsMileage);
        locationDataPoint.setDiffFuelCons(integralFuelConsumption);
        locationDataPoint.setDiffMileage(differentialMileage);
        locationDataPoint.setEcuMileage(ecuMileage);
        locationDataPoint.setMeterMileage(mileageDD);
        locationDataPoint.setTotalFuelCons(totalFuelConsumption);
        locationDataPoint.setRotation(rotation);
        locationDataPoint.setEngineTorMode(engineTorMode);
        locationDataPoint.setAccPedalPos(accPedalPos);
        locationDataPoint.setHeight(height);
        locationDataPoint.setLat(lat);
        locationDataPoint.setLng(lng);
        locationDataPoint.setActualEnginePercentTor(actualEnginePercentTor);
        locationDataPoint.setCoolingWaterTem(coolingWaterTem);
        locationDataPoint.setIntakeAirTem(intakeAirTem);
        locationDataPoint.setDpfPressure(dpfPressure);
        locationDataPoint.setClutchSwitch(clutchSwitch);
        return locationDataPoint;
    }

    public Point toPoint() {
        Point p = new Point();
        p.setTid(this.getTid());
        p.setDiffFuelCons(this.getDiffFuelCons());
        p.setDiffMileage(this.getDiffMileage());
        p.setEcuMileage(this.getEcuMileage());
        p.setGpsDate(this.getGpsDate() * 1000);
        p.setGpsMileage(this.getGpsMileage());
        p.setRotation(this.getRotation());
        p.setMeterMileage(this.getMeterMileage());
        p.setTotalFuelCons(this.getTotalFuelCons());
        return p;
    }
}
